/* 
 * File:   main.c
 * Author: Jeff
 *
 * Created on April 21, 2014, 6:03 PM
 */

#include <pic18f24k50.h>
#include <xc.h>
#include "config.h"

#define pin0 PORTCbits.RC2
#define pin1 PORTCbits.RC1
#define pin2 PORTCbits.RC0
#define pin3 PORTAbits.RA6
#define pin4 PORTCbits.RC6
#define pin5 PORTCbits.RC7
#define pin6 PORTBbits.RB0
#define pin7 PORTBbits.RB1
#define pin8 PORTBbits.RB2
#define pin9 PORTBbits.RB3
#define pin10 PORTBbits.RB4
#define pin11 PORTBbits.RB5
#define pin12 PORTBbits.RB6
#define pin13 PORTBbits.RB7
#define pinA5 PORTAbits.RA4

#define dataOut PORTAbits.RA7

#define _XTAL_FREQ 16000000

/*
 * 
 */
unsigned char ad_convert8bit(unsigned char channel) {
    //ADRESH = 0;
    ADCON0bits.CHS = channel;
    ADCON0bits.GO = 1;
    while(ADCON0bits.GO == 1);
    return ADRESH;
}

void writeByte(unsigned char c) {
    dataOut = 0;
    unsigned char i = 8;
    _delay(5);
    do{
        if(c & 0b00000001)
            dataOut = 1;
        else {
            dataOut = 0;
            _delay(3); // compiler optimization timing correction
        }
        c = c >> 1;
    }while(--i);
    _delay(4);
    dataOut = 1;
    _delay(2);
}


int main() {

    OSCCONbits.IRCF = 0b111; // 16MHz internal oscillator = 4MHz clock

    ANSELA = 0b00101111; // turn on analog input mode
    ANSELB = 0b00000000;
    ANSELC = 0b00000000;
    TRISA = 0b01111111; // all of port A is input except RA7
    TRISB = 0b11111111; // all of port B is input
    TRISC = 0b11000111; // almost all of port C is input besides the USB pins

    ADCON0bits.ADON = 1;
    ADCON1 = 0b00000000; // Vss and Vdd references
    ADCON2 = 0b000010101; // left justified, 4TAD acquisition time, Fosc/16 conversion clock

    while(1) {

        writeByte(0b10101010); // start byte to line up buffer
        // fill the first 5 bytes with analog pin data (8 bit only)
        for(unsigned char i = 0; i < 5; i++) {
            //*(pinStates+i) |= (ad_convert8bit(i)); // load the 5 10 bit adc values into the pinStates buffer
            writeByte(ad_convert8bit(i));
            //writeByte(0b11101010);
        }

        // would be faster, but would sending the extra byte slow it down?
        //_delay(30);
        writeByte(PORTA & 0x7F);
        //_delay(30);
        writeByte(PORTB);
        //_delay(30);
        writeByte(PORTC);

        /*
        tmpChar1 |= (unsigned char)(pinA5 * 128);
        tmpChar1 |= (unsigned char)(pin0 * 64);
        tmpChar1 |= (unsigned char)(pin1 * 32);
        tmpChar1 |= (unsigned char)(pin2 * 16);
        tmpChar1 |= (unsigned char)(pin3 * 8);
        tmpChar1 |= (unsigned char)(pin4 * 4);
        tmpChar1 |= (unsigned char)(pin5 * 2);
        tmpChar1 |= (unsigned char)(pin6);

        writeByte(tmpChar1);

        tmpChar2 |= (unsigned char)(pin7 * 128);
        tmpChar2 |= (unsigned char)(pin8 * 64);
        tmpChar2 |= (unsigned char)(pin9 * 32);
        //tmpChar2 |= 32;
        tmpChar2 |= (unsigned char)(pin10 * 16);
        tmpChar2 |= (unsigned char)(pin11 * 8);
        tmpChar2 |= (unsigned char)(pin12 * 4);
        tmpChar2 |= (unsigned char)(pin13 * 2);

        writeByte(tmpChar2);
        */
        
        __delay_ms(2);

    }
    
    return 0;
}